#include <iostream>
#include <set>
#include <utility>
#include <string>
#include <vector>

using std::cout;
using std::endl;
using std::set;
using std::pair;
using std::string;
using std::vector;

void test()
{
    //set的基本特征
    //1、存放的是key值，key值是唯一的，不能重复
    //2、默认情况下，会按照key进行升序排列
    //3、底层使用的是红黑树
    /* set<int> number();//error，不是创建对象, number是函数名*/
    /* set<int> number = {1, 3, 5, 9, 7, 4, 2, 6, 7}; */

    int arr[10] = {1, 2, 3, 5, 7, 9, 8, 7, 6, 10};
    set<int> number(arr, arr + 10);//左闭右开的区间[,)

    //迭代器，可以看成是指针（广义的指针）
    set<int>::iterator it;
    for(it = number.begin(); it != number.end(); ++it)
    {
        cout << *it << "  ";
    }
    cout << endl;

    for(auto &elem : number)
    {
        cout << elem << "  ";
    }
    cout << endl;

    cout << endl << "set的查找操作" << endl;
    size_t cnt = number.count(17);
    cout << "cnt = " << cnt << endl;

    set<int>::iterator it2 = number.find(7);
    /* if(it2 == nullptr)//error */
    if(it2 == number.end())
    {
        cout << "该元素不存在set中" << endl;
    }
    else
    {
        cout << "该元素存在set中 " << *it2 << endl;
    }

    cout << endl << "set的insert操作" << endl;
    /* pair<set<int>::iterator, bool> ret = number.insert(4); */
    auto ret = number.insert(4);
    if(ret.second)
    {
        cout << "插入成功 " << *ret.first << endl;
    }
    else
    {
        cout << "插入失败,该元素存在set中" << endl;
    }

    vector<int> vec = {12, 45, 56, 78, 13, 3, 90};
    number.insert(vec.begin(), vec.end());
    for(auto &elem : number)
    {
        cout << elem << "  ";
    }
    cout << endl;

    cout << endl << endl;
    /* cout << "number[1] = " << number[1] << endl;//error */

    auto it3 = number.begin();
    cout << "*it3 = " << *it3 << endl;
    /* *it3 = 100;//error */
}

void test2()
{
    pair<int, string> number = {1, "hello"};
    cout << number.first << "----->" << number.second << endl;
}
int main(int argc, char *argv[])
{
    test();
    return 0;
}

